﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Arc.DairyMan.DairyMan.Model;

namespace Arc.DairyMan.DairyMan.DataAccess
{
    public class dalSabhasadTransactions : AbsDal
    {
        public override int Transaction()
        {
            try
            {
                int Result = 0;
                SqlCommand cmd = Connection.CreateCommand();
                SabhasadTransactions _SabhasadTransactions = (SabhasadTransactions)ModelObj;
                switch (_SabhasadTransactions.TransactionMode)
                {
                    case "I":
                        #region Insert
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = _SabhasadTransactions.FriendlyName + "_" + _SabhasadTransactions.TransactionMode;
                        //_Sabhasad.PrimaryKey = Convert.ToInt32(cmd.ExecuteScalar());
                        //if (Convert.ToInt32(_ModelObj.PrimaryKey) > 0) 

                        cmd.Parameters.Add(new SqlParameter("@SabhasadId", _SabhasadTransactions.SabhasadId));
                        cmd.Parameters.Add(new SqlParameter("@Date", _SabhasadTransactions.Date));
                        cmd.Parameters.Add(new SqlParameter("@TransactionType", _SabhasadTransactions.TransactionType));
                        cmd.Parameters.Add(new SqlParameter("@Amount", _SabhasadTransactions.Amount));

                        Result = ExecuteCommand(cmd);

                        break;
                        #endregion
                    case "U":
                        #region Update
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = _SabhasadTransactions.FriendlyName + "_" + _SabhasadTransactions.TransactionMode;
                        //_Sabhasad.PrimaryKey = Convert.ToInt32(cmd.ExecuteScalar());
                        //if (Convert.ToInt32(_ModelObj.PrimaryKey) > 0) 

                        cmd.Parameters.Add(new SqlParameter("@SabhasadTransactionId", _SabhasadTransactions.SabhasadTransactionId));
                        cmd.Parameters.Add(new SqlParameter("@SabhasadId", _SabhasadTransactions.SabhasadId));
                        cmd.Parameters.Add(new SqlParameter("@Date", _SabhasadTransactions.Date));
                        cmd.Parameters.Add(new SqlParameter("@TransactionType", _SabhasadTransactions.TransactionType));
                        cmd.Parameters.Add(new SqlParameter("@Amount", _SabhasadTransactions.Amount));

                        Result = ExecuteCommand(cmd); ;

                        break;
                        #endregion
                    case "D":
                        #region Delete
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = _SabhasadTransactions.FriendlyName + "_" + _SabhasadTransactions.TransactionMode;

                        cmd.Parameters.Add(new SqlParameter("@SabhasadTransactionId", _SabhasadTransactions.PrimaryKey));

                        Result = ExecuteCommand(cmd); ;

                        break;
                        #endregion
                    default:
                        throw new Exception("Invalid Transaction Mode:" + _SabhasadTransactions.TransactionMode);

                }
                return Result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override void Select(string Critiera, DataSet Ds)
        {
            try
            {
                string _SQL = @"Select * from " + ModelObj.FriendlyName + "";
                if (Critiera.Trim() != string.Empty)
                    _SQL += " Where " + Critiera;
                GetDataTable(_SQL, ModelObj.FriendlyName, Ds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override void Search(string Critiera, DataSet Ds)
        {
            try
            {
                string _SQL = @"Select * from " + ModelObj.FriendlyName + "";
                if (Critiera.Trim() != "")
                    _SQL += " Where " + Critiera;
                GetDataTable(_SQL, ModelObj.FriendlyName, Ds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
